package com.nep.controller;

import com.nep.entity.Supervisor;
import com.nep.service.SupervisorService;
import com.nep.service.impl.SupervisorServiceImpl;
import com.nep.util.JavafxUtil;
import com.nep.NepsMain;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class NepsRegisterViewController {
    @FXML
    private TextField txt_id;
    @FXML
    private PasswordField txt_password;
    @FXML
    private PasswordField txt_repassword;
    @FXML
    private TextField txt_realName;
    @FXML
    private RadioButton txt_sex;
    @FXML
    private ToggleGroup sex;
    @FXML
    private TextField txt_phone;
    
    //主舞台
    public static Stage primaryStage;
    //多态
    private SupervisorService supervisorService = new SupervisorServiceImpl();

    @FXML
    public void initialize() {
        // 初始化时设置默认选中的单选按钮
        if (txt_sex != null) {
            txt_sex.setSelected(true);
        }
    }

    public TextField getTxt_id() {
        return txt_id;
    }
    public void setTxt_id(TextField txt_id) {
        this.txt_id = txt_id;
    }
    public PasswordField getTxt_password() {
        return txt_password;
    }
    public void setTxt_password(PasswordField txt_password) {
        this.txt_password = txt_password;
    }
    public PasswordField getTxt_repassword() {
        return txt_repassword;
    }
    public void setTxt_repassword(PasswordField txt_repassword) {
        this.txt_repassword = txt_repassword;
    }
    public TextField getTxt_realName() {
        return txt_realName;
    }
    public void setTxt_realName(TextField txt_realName) {
        this.txt_realName = txt_realName;
    }
    public TextField getTxt_phone() {
        return txt_phone;
    }
    public void setTxt_phone(TextField txt_phone) {
        this.txt_phone = txt_phone;
    }

    @FXML
    public void register() {
        if (txt_id.getText().isEmpty() || txt_password.getText().isEmpty() || 
            txt_repassword.getText().isEmpty() || txt_realName.getText().isEmpty() ||
            txt_phone.getText().isEmpty()) {
            JavafxUtil.showAlert(primaryStage, "注册失败", "请填写所有必填项", "", "warn");
            return;
        }

        if (!txt_password.getText().equals(txt_repassword.getText())) {
            JavafxUtil.showAlert(primaryStage, "注册失败", "两次输入密码不一致", "请重新输入确认密码", "warn");
            txt_repassword.setText("");
            return;
        }

        Supervisor supervisor = new Supervisor();
        supervisor.setLoginCode(txt_id.getText());
        supervisor.setPassword(txt_password.getText());
        supervisor.setRealName(txt_realName.getText());
        supervisor.setPhone(txt_phone.getText());
        // 获取选中的性别
        String selectedSex = txt_sex.isSelected() ? "男" : "女";
        supervisor.setSex(selectedSex);
        
        boolean flag = supervisorService.register(supervisor);
        if (flag) {
            JavafxUtil.showAlert(primaryStage, "注册成功", txt_id.getText() + " 账号注册成功!", "可以进行用户登录!", "info");
            //跳转到登录界面进行登录
            JavafxUtil.showStage(NepsRegisterViewController.class, "/com/nep/view/NepsLoginView.fxml", primaryStage, "东软环保公众监督平台-公众监督员端");
        } else {
            JavafxUtil.showAlert(primaryStage, "注册失败", "手机号已被注册", "请重新输入注册手机号码", "warn");
            txt_id.setText("");
        }
    }

    @FXML
    public void back() {
        JavafxUtil.showStage(NepsRegisterViewController.class, "/com/nep/view/NepsLoginView.fxml", primaryStage, "东软环保公众监督平台-公众监督员端");
    }
}
